// import log from '@dfeidao/atom-nodejs/logger/log';
import query from '@dfeidao/atom-nodejs/db/query';
import format from '@dfeidao/atom-nodejs/dt/format';
import { IncomingHttpHeaders } from 'http';
import { productid, systemid } from '../../atom/config';

interface Message {
	plan_no: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	// log('Service begin path:c:\Users\32645\Desktop\kh\src\plan-cb\zj-000003\s001,action_id:' + action_id);

	// log('Service end path:c:\Users\32645\Desktop\kh\src\plan-cb\zj-000003\s001,action_id:' + action_id);

	// plan_type_no	计划类型编号
	// plan_name	计划名称
	// plan_describe	计划描述
	// plan_cycle	计划周期
	// plan_type_name	计划类型名称
	// plan_cla_attr_no	计划分类属性编号
	// plan_cla_attr_name	计划分类属性名称
	// start_date	开始日期
	// end_date	结束日期
	// call_period	调用期
	// plan_mark	计划标识
	// plan_list_no	计划清单编号
	// take_effect	是否生效
	// plan_list_name	计划清单名称
	// plan_implement_status	计划实施状态
	// plan_location	计划地点
	// request_date	关键日期
	// plan_no	计划编号
	// productid	产品ID


	const { prepare, exec } = query(action_id, session_id, systemid);
	prepare('plan', ['plan_name', 'plan_describe', 'plan_cycle', 'plan_type_no', 'plan_type_name', 'plan_cla_attr_no', 'plan_cla_attr_name', 'start_date', 'end_date', 'call_period', 'plan_mark', 'plan_list_no', 'plan_list_name', 'plan_implement_status', 'take_effect', 'plan_location', 'request_date'], { productid, plan_no: message.plan_no }, 200, 1, [], []);
	const [res11] = await exec<{ start_date: string; end_date: string; }>();

	const res = res11.map((v) => {
		const nowdate1 = new Date(v.start_date).getTime();
		const newdate1 = format(nowdate1, 'YYYY-MM-DD');
		const nowdate2 = new Date(v.end_date).getTime();
		const newdate2 = format(nowdate2, 'YYYY-MM-DD');
		return { ...v, newdate1, newdate2 };
	});

	const q1 = query(action_id, session_id, systemid);
	q1.prepare('plan_classify', ['plan_cla_attr_no', 'plan_cla_attr_name'], { productid }, 200, 1, [], []);
	const [res1] = await q1.exec();

	return {
		data: { res, res1 }
	};
}
